package org.esprit.ocm.client.ui.ec2;

import java.util.List;

import org.esprit.ocm.client.model.ec2.datasource.KeyPairLocalDS;
import org.esprit.ocm.client.rpc.ec2.KeyPairService;
import org.esprit.ocm.client.rpc.ec2.KeyPairServiceAsync;
import org.esprit.ocm.dto.ec2.impl.KeyPairInfoMappingDto;
import org.esprit.ocm.dto.impl.AwsCredentialsDto;
import org.esprit.ocm.dto.impl.ServerDto;
import org.esprit.ocm.dto.impl.UserDto;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.toolbar.ToolStrip;

public class KeyPairZone extends SectionStack {

	private final KeyPairServiceAsync	keyPairService	= GWT.create(KeyPairService.class);

	private DataSource					ds;
	private ListGrid					grid;
	private DataSource					dsPermission;
	private ListGrid					gridPermission;

	public KeyPairZone(final ServerDto server, final AwsCredentialsDto credentials, final UserDto user) {

		getData(server, credentials);

		// creer groupSection
		SectionStackSection keyPairSection = new SectionStackSection("Group List");
		keyPairSection.setExpanded(true);
		keyPairSection.setCanCollapse(false);
		addSection(keyPairSection);

		// creer groupGrid
		grid = new ListGrid();
		setVisibilityMode(VisibilityMode.MULTIPLE);
		setHeaderHeight(20);
		grid.setShowFilterEditor(true);
		grid.setFilterOnKeypress(true);
		grid.setAutoFetchData(true);
		
		gridPermission = new ListGrid();
		gridPermission.setShowFilterEditor(true);
		gridPermission.setFilterOnKeypress(true);
		gridPermission.setAutoFetchData(true);

		// creer grouptoolstrip
		ToolStrip keyPairGridToolStrip = new ToolStrip();
		keyPairGridToolStrip.setWidth100();
		keyPairGridToolStrip.addFill();

		// ajouter toolstrip a instanceSection
		keyPairSection.addItem(keyPairGridToolStrip);

		final Button addKeyPairButton = new Button("Add KeyPair");
		addKeyPairButton.setShowRollOver(true);
		addKeyPairButton.setShowDown(true);
		addKeyPairButton.setIcon("add.png");
		addKeyPairButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new AddKeyPair(server, credentials, user);
			}
		});
		
		final Button deleteKeyPairButton = new Button("Delete KeyPair");
		deleteKeyPairButton.setShowRollOver(true);
		deleteKeyPairButton.setShowDown(true);
		deleteKeyPairButton.setIcon("remove.png");
		deleteKeyPairButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				String name = grid.getSelectedRecord().getAttribute("keyName");
				keyPairService.deleteKeypair(name, server, credentials, new AsyncCallback<Boolean>() {
					
					public void onSuccess(Boolean arg0) {
						SC.say("deleted");
						ds.destroy();
						getData(server, credentials);
						grid.redraw();
					}
					
					public void onFailure(Throwable arg0) {
						
					}
				});
			}
		});
		
		Img filter = new Img("filter.png", 15, 15);

		// creer filterButton
		final Button filterButtonOn = new Button("On");
		filterButtonOn.setWidth(25);
		filterButtonOn.setHeight(20);
		filterButtonOn.setShowRollOver(true);
		filterButtonOn.setShowDown(true);
		// filterButtonOn.setIcon("filter.png");
		filterButtonOn.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {

				grid.setShowFilterEditor(true);
				grid.setFilterOnKeypress(true);
			}
		});
		// creer filterButton
		final Button filterButtonOff = new Button("Off");
		filterButtonOff.setWidth(25);
		filterButtonOff.setHeight(20);
		filterButtonOff.setShowRollOver(true);
		filterButtonOff.setShowDown(true);
		// filterButtonOff.setIcon("filter.png");
		filterButtonOff.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {

				grid.setShowFilterEditor(false);
				grid.setFilterOnKeypress(false);
			}
		});

		final Button refreshButton = new Button("Refresh");
		refreshButton.setShowRollOver(true);
		refreshButton.setShowDown(true);
		refreshButton.setIcon("interact.png");
		refreshButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				SC.say("Refreshed");
				ds.destroy();
				getData(server, credentials);
				grid.redraw();
			}
		});

		// ajouter le bouton
		keyPairGridToolStrip.addMember(addKeyPairButton);
		keyPairGridToolStrip.addMember(deleteKeyPairButton);
		keyPairGridToolStrip.addMember(refreshButton);
		keyPairGridToolStrip.addSpacer(10);
		keyPairGridToolStrip.addMember(filter);
		keyPairGridToolStrip.addSpacer(2);
		keyPairGridToolStrip.addMember(filterButtonOn);
		keyPairGridToolStrip.addMember(filterButtonOff);
		keyPairGridToolStrip.addSpacer(5);

		// ajouter instanceGrid a instanceSection
		keyPairSection.addItem(grid);

		draw();
	}

	private void getData(ServerDto server, AwsCredentialsDto credentials) {
		keyPairService.listKeypairs(server, credentials, new AsyncCallback<List<KeyPairInfoMappingDto>>() {

			public void onSuccess(List<KeyPairInfoMappingDto> list) {

				for (KeyPairInfoMappingDto k : list) {
					System.out.println(k.getKeyName());
				}
				ds = KeyPairLocalDS.getInstance(list);
				grid.setDataSource(ds);
				
				ListGridField idField = new ListGridField("keyName", "keyName");
				ListGridField descriptionField = new ListGridField("keyFingerprint", "keyFingerprint");

				grid.setFields(idField, descriptionField);

			}

			public void onFailure(Throwable arg0) {
			}
		});
	}
}
